const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    imgUrl: SERVICE.imgUrl,
    support_stauts: false,
    shareData: {
      show: false,
      quit:"shareQuit",
      share: "Share"
    },
  },
  onLoad(options){
   this.setData({
     options: options
   });
    this.shareInfo();
  },


  /*分享*/ 
  onShareAppMessage(){
    let td = this.data;
    return {
      title: td.title,
      imageUrl: td.imgUrl + td.shareImgSrc,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  
  shareInfo(){
    let _this = this, td = this.data;
    wx.showNavigationBarLoading();
    //获取分享信息 
    SERVICE.POST(SERVICE.URL.shareRecruitment, {
      recruitment_id: td.options.id,
      code: wx.getStorageSync("loginInfo").code
    }, (status, res) => {
      console.log("获取分享信息:",res);
      wx.hideNavigationBarLoading();
      if (res.data.status == "success") {
        wx.setNavigationBarTitle({
          title: res.data.userExtraInfo.realName + '的招聘'
        });
        _this.setData({
          shareRecruitmentInfo: res.data,
          title: res.data.userExtraInfo.realName + '的招聘',
          id: res.data.recruitment_detail.id
        });
        _this.shareRecruitImage(res.data.recruitment_detail.id);
      } else {
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /*点赞*/ 
  support(e){
    wx.showLoading();
    let _this = this;
    wx.login({
      success: function(code){
        wx.setStorageSync("loginInfo", code.code);
        SERVICE.POST(SERVICE.URL.support, {
          code: code.code,
          recruitment_id: e.currentTarget.dataset.id
        }, (status, res) => {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true,
            duration: 1500,
            success: function () {
              _this.setData({
                support_stauts: res.data.support_stauts,
              });
              _this.shareInfo();
            }
          });
        });
      }
    })
    
  },

  /*打电话*/ 
  tel(e){
    wx.showModal({
      title: "提示",
      content: "是否联系招聘人",
      success(res){
        if(res.confirm){
          wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.tel
          })
        }
      }
    })
  },

  /*分享按钮*/
  share() {
    this.data.shareData.show = true;
    this.setData({
      shareData: this.data.shareData
    });
  },

  /*分享退出按钮*/
  shareQuit() {
    this.data.shareData.show = false;
    this.setData({
      shareData: this.data.shareData
    });
  },

  Share(){
    let td = this.data;
    wx.navigateTo({
      url: "../share-page/share-page?type=1&id=" + td.id,
    })
  },

  // 招聘
  shareRecruitImage(id) {
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.shareRecruitImage, {
      recruitment_id: id,
      genre: 'flockPic'
    }, (status, res) => {
      console.log(res);
      _this.setData({
        shareImgSrc: res.data
      });
    });
  }
});